Iterations = 500
##change filename here for different set
filename = "~/alive/rsalgos/tests/results/unif1"
SizeOfP = 5000
SizeOfS = 30
M = 5

library("rsdepth")
library("depth")


MedianTable = matrix(rnorm(Iterations*8),nc=8)


pdf(paste(filename,".pdf",sep=""))

##TODO: comment out following lines
pl=matrix(c(-2.0,2.0,-2.0,2.0),nc=2)
plot(pl)

for (j in 1:Iterations )
{

pt1 = matrix(runif(2),nc=2)
pt2 = matrix(runif(2),nc=2)
pt3 = matrix(runif(2),nc=2)
pt4 = matrix(runif(2),nc=2)

#Change distribution here
	#P1 = matrix(rcauchy(0.95*SizeOfP),nc=2)
	#P2 = matrix(rnorm(0.05*SizeOfP,1),nc=2)
	#P=rbind(P1,P2)
	P = matrix(runif(SizeOfP*2),nc=2)
	
	 x=P[,1]
	 y=P[,2]
	 
	for (f in 1:M )
	{
 
		index = floor(runif(SizeOfS,1,SizeOfP))
		 
		sampleX=x[index]
		sampleY=y[index]

		maxrs=-1
		maxtukey=-1
		maxliu=-1
		maxoja=-1
	
	
		########rs
		pt=cbind(sampleX,sampleY)
		#print(date())
		temp=rsmed(pt)
		dp=rsdepth(pt,pt1)
		if( dp>maxrs )
		{
			maxrs=dp;
			pt1=temp;
		}
	
		########tukey
		#print(date())
		temp=med(pt,method="Tukey",mustdith=T)
	
		if( temp$depth > maxtukey )
		{
			maxtukey=temp$depth;
			pt2=temp$median;
		}
	
	
		########liu
		#print(date())
		temp=med(pt,method="Liu",mustdith=T)
	
		if( temp$depth > maxliu )
		{
			maxliu=temp$depth;
			pt3=temp$median;
		}
	
	
		########oja
		#print(date())
		temp=med(pt,method="Oja",mustdith=T)
	
		if( temp$depth > maxoja )
		{
			maxoja=temp$depth;
			pt4=temp$median;
		}
	}	
	

	points(pt1[1],pt1[2],col="blue",pch="x")
	points(pt2[1],pt2[2],col="red",pch="+")
	points(pt3[1],pt3[2],col="green",pch="x")
	points(pt4[1],pt4[2],col="yellow",pch="x")
	
	MedianTable[j,1]=pt1[1]
	MedianTable[j,2]=pt1[2]
	MedianTable[j,3]=pt2[1]
	MedianTable[j,4]=pt2[2]
	MedianTable[j,5]=pt3[1]
	MedianTable[j,6]=pt3[2]	
	MedianTable[j,7]=pt4[1]
	MedianTable[j,8]=pt4[2]
	
	print(j)


}
write.table(MedianTable,paste(filename,".txt",sep=""),row.names=F,col.names=F)
dev.off()
